package execd1126;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class Test {
    public static void main(String[] args) {
        Person p1 = new Person("刘备","魏国");
        Person p2 = new Person("关羽","魏国");
        Person p3 = new Person("曹操","魏国");
        Person p4 = new Person("张飞","魏国");
        Person p5 = new Person("黄忠","魏国");
        List<Person> list = new ArrayList();
        Collections.addAll(list,p1,p2,p3,p4,p5);
//        有条件删除，非蜀国人删除
        System.out.println("*******迭代器修改*******");
        Iterator<Person> it = list.iterator();
        while(it.hasNext()){
            Person p = it.next();
            if(!p.getCountry().equals("蜀国")){
                p.setCountry("蜀国");
            }
        }
        System.out.println(list);
        System.out.println("*******forEach*******");
        list.forEach(new Consumer<Person>() {
            @Override
            public void accept(Person p) {
                if(!p.getCountry().equals("蜀国")){
                  p.setCountry("蜀国");
                }
            }
        });
        System.out.println(list);
        //传统for循环
        System.out.println("******传统for循环******");
        for (int i = 0; i < list.size(); i++) {
            Person p = list.get(i);
            if(!p.getCountry().equals("蜀国")) {
                p.setCountry("蜀国");
            }
        }
        System.out.println(list);

        //增强for
        System.out.println("******增强for循环******");
        for(Person p :list){
            if(!p.getCountry().equals("蜀国")){
                p.setCountry("蜀国");
            }
        }
        System.out.println(list);
    }

    }

